#!/bin/bash
echo "start sync gameconfig "
svn_branch=$1
git_branch=$2
if [[ -z $git_branch ]]; then
    git_branch=$svn_branch
fi
dir=./
cd $dir
echo "svn=$svn_branch git=$git_branch"
svn_path=$(jq -r ".$svn_branch" svn_config.json)
if [ -z $svn_path ]; then
    echo "no svn_path"
    exit 1
fi
echo "svn_path=$svn_path"
svn_dir=gameconfig-svn-$svn_branch

rm -rf $dir/$svn_dir
svn co -q $svn_path $svn_dir

cd $dir/gameconfig-git
git restore .
git fetch
git checkout $git_branch
git pull --rebase
rm -rf ./*.config

echo "rsync start"
rsync -av -q --progress $dir/$svn_dir/ $dir/gameconfig-git/ --exclude .svn
echo "rsync end"

st=$(git status -s)

if [ -z "$st" ]; then
    echo "nothing changed"
else
    git add .
    git commit -m "auto sync"
    git push
    echo "sync success"
fi
rm -rf $dir/$svn_dir
